package xyz.wadewhy.basicstest.kotlin

import com.alibaba.fastjson.JSON
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import xyz.wadewhy.basicstest.easyExcel.test.DemoData
import java.util.*
import java.util.concurrent.Callable
import java.util.concurrent.CountDownLatch

fun main() {
//    test()
//    test12()
    test32()
}

fun test() {
    var sheetIndex = 0
    println(sheetIndex)
    val s = sheetIndex.inc()
    println(sheetIndex)
    println(s)
    var sheetIndex1 = 0
    println(sheetIndex1)
    test1(++sheetIndex1)
}

fun test1(s: Int) {
    println(s)
}


fun test12() {
    val list = Vector<DemoData>()

    Thread({
        list.add(DemoData("wade", Date(), 2.0))

    }, "线程").start()
    Thread({
        list.add(DemoData("awade", Date(), 1.0))

    }).start()
    Thread({
        list.add(DemoData("ewade", Date(), 3.0))
    }).start()
    println(JSON.toJSONString(list))

    println(JSON.toJSONString(list.sortBy { it.doubleData }))

    println(JSON.toJSONString(list.toList().sortedBy { it.string }))
}

fun test32() {
    val threadPoolTaskExecutor = ThreadPoolTaskExecutor()
    threadPoolTaskExecutor.initialize(); //必须初始化，否则报 ThreadPoolTaskExecutor not initialized
    threadPoolTaskExecutor.corePoolSize = 10;//默认1
    threadPoolTaskExecutor.maxPoolSize = 30;
    val listof = listOf<Int>(1, 2, 3, 4)
    val list = Vector<DemoData>()
    listOf<Int>(1, 2, 3, 4, 5, 6).map {
        println(listof.size)
        val endGate = CountDownLatch(listof.size)
        listof.forEachIndexed { index, i ->
         try {
             val result = Callable {
                 println("里面哦")
                 list.add(DemoData("awade", Date(), 1.0 + index))
             }
             threadPoolTaskExecutor.submit(result)
         }finally {
             endGate.countDown()
         }

        }
        endGate.await()
    }
    println("主线程......")
    println(JSON.toJSONString(list))
    println(JSON.toJSONString(list.toList()))
}
